﻿using System.Linq;
using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Swtor;
using Buddy.Swtor.Objects;
using WingIt.Dynamics;

namespace WingIt.Routines
{
    //09.05.2012 Pios
    public static class SorcererMadness
    {
        [Behavior(BehaviorType.Pull)]
        [Class(CharacterClass.Inquisitor, AdvancedClass.Sorcerer, SkillTreeId.SorcererMadness)]
        public static Composite SorcererMadnessPull()
        {
            return new PrioritySelector(
                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 29f / 10f),
                SorcererMadnessCombat()
                );
        }

        [Behavior(BehaviorType.Combat)]
        [Class(CharacterClass.Inquisitor, AdvancedClass.Sorcerer, SkillTreeId.SorcererMadness)]
        public static Composite SorcererMadnessCombat()
        {
            return new PrioritySelector(
                Spell.Cast("Force of Will", castWhen => BuddyTor.Me.IsStunned),
                CommonBuffsAndHealing(),
                Spell.WaitForCast(),
                Spell.Cast("Crushing Darkness",
                           ret => BuddyTor.Me.HasBuff("Wrath") &&
                                  !BuddyTor.Me.CurrentTarget.HasDebuff("Crushing Darkness")),
                Spell.Cast("Affliction", onUnit =>
                                             {
                                                 var previousTarget = BuddyTor.Me.CurrentTarget;
                                                 return
                                                     Helpers.Targets.FirstOrDefault(
                                                         targ =>
                                                         targ != previousTarget &&
                                                         (targ.Toughness != CombatToughness.Player) &&
                                                             // Dont go all pvp on us.
                                                         !targ.IsDead &&
                                                         !targ.HasDebuff("Affliction (Force)")) ??
                                                     Helpers.Targets.FirstOrDefault(targ => targ != previousTarget);
                                             }, castWhen => Helpers.Targets.Count() >= 1),
                Spell.Cast("Creaping Terror",
                           ret => !BuddyTor.Me.CurrentTarget.HasDebuff("Creaping Terror")),
                Spell.CastOnGround("Death Field",
                                   ret => BuddyTor.Me.CurrentTarget.Distance <= 0.5f &&
                                          BuddyTor.Me.ResourceStat >= 100,
                                   location => BuddyTor.Me.CurrentTarget.Position),
                Spell.Cast("Recklessness"), // Lets get recklessness up.
                new Decorator( // Only cast shock if we are moving.
                    ret => BuddyTor.Me.IsMoving,
                    Spell.Cast("Shock")),
                Spell.Cast("Force Lightning"), // 3 Second channel
                Spell.WaitForCast(),
                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 2.8f));
        }

        public static Composite CommonBuffsAndHealing()
        {
            return
                new PrioritySelector(
                    Spell.Cast("Static Barrier",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      !Helpers.Companion.HasBuff("Static Barrier") &&
                                      !Helpers.Companion.HasDebuff("Deionized")),
                    Spell.Cast("Static Barrier",
                               onWhoToCast => BuddyTor.Me,
                               ret => !BuddyTor.Me.HasBuff("Static Barrier") &&
                                      !BuddyTor.Me.HasDebuff("Deionized")),
                    Spell.Cast("Resurgence",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      Helpers.Companion.HealthPercent < 90),
                    Spell.Cast("Resurgence",
                               onWhoToCast => BuddyTor.Me,
                               ret => BuddyTor.Me.HealthPercent < 90),
                    Spell.Cast("Innervate",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      Helpers.Companion.HealthPercent < 80),
                    Spell.Cast("Innervate",
                               onWhoToCast => BuddyTor.Me,
                               ret => BuddyTor.Me.HealthPercent < 80),
                    Spell.Cast("Dark Heal",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      Helpers.Companion.HealthPercent < 60),
                    Spell.Cast("Dark Heal",
                               onWhoToCast => BuddyTor.Me,
                               ret => BuddyTor.Me.HealthPercent < 80),
                    Spell.Cast("Dark Infusion",
                               onWhoToCast => Helpers.Companion,
                               ret => Helpers.Companion != null &&
                                      Helpers.Companion.HealthPercent < 55),
                    Spell.Cast("Dark Infusion",
                               onWhoToCast => BuddyTor.Me,
                               ret => BuddyTor.Me.HealthPercent < 65));
        }
    }
}
